//
//  Ugc_HomePage_TbV_V3.m
//  ZITHER
//
//  Created by weixin on 2018/10/23.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "Ugc_HomePage_TbV_V3.h"

#import "MainFirst_MagazineList_UgcCell_WX.h"
#import "MainFirst_MagazineList_PGCCell_WX.h"


#import "MainFirst_Magazine_VC_WX.h"
#import "Ugc_Detail_VC_WX.h"
#import "UgcMainModel.h"

@implementation Ugc_HomePage_TbV_V3


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
    }
    return self;
}

-(void)Starefreshing{
    [self setLoad];
}

-(void)setLoad{
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
    @weakify(self);
    
    [[serviceDefualt loadMainCreateListuUserId:self.userId page:self.page reponseObjctClass:[CoverCreateListModel class]]subscribeNext:^(ResponseData * x) {
        @strongify(self);
        if (x.success) {
            [self setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];

            CoverCreateListModel * model = [[CoverCreateListModel alloc]init];
            model.recommend = @"0";
            CoverResultModelSub *mdoel1 = [CoverResultModelSub yy_modelWithDictionary:x.responseRawData[@"data"]];
            model.result = mdoel1;
//            CoverCreateListModel * model = x.responseData;
            
            if (model.result.list.count == 0) {
                
                if (self.page == 0 ) {
                    
                    if ([self.userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
                        [self setNoMoreDataBg:NO NoDataState:NoMoreDataStatePublish];
                    }else{
                        [self setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                        self.noMoreDateView.frame = CGRectMake(0, 110, kScreenW, kScreenH);
                        [self.HeadView isNoMoreDate:YES];
                    }
                    [self setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                    
                    self.noMoreDateView.title.text = @"还没有发布过创作";
//                    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 90+20)];
                   
                    [Tools showTips:@"暂无更多数据"];
                    [self endRefreshing];
                    [self.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
                
            }else{
                [self.HeadView isNoMoreDate:NO];
                if (self.page == 0) {
                    self.model = model;
                }else{
                    for (CoverCreateListModelSub *modelSub in model.result.list) {
                        [self.model.result.list addObject:modelSub];
                    }
                }
            }
            [self endRefreshing];
            [self reloadData];
        }else{
            [Tools showTips:x.errorInfo];
            [self endRefreshing];
        }
    }];
    
}


-(void)defaultConfiguration{
    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 20)];
    self.delegate = self;
    self.dataSource = self;
    //    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    self.rowHeight = UITableViewAutomaticDimension;
    //    self.estimatedRowHeight = 1000;
}


#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.result.list.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* TODO: ================= 判断 cell 样式 =================*/
    //        UgcMainListModel *model = self.arrM[indexPath.row];
    //        NSString *sectionCell = [NSString stringWithFormat:@"UgcFriendsTbvCell%ld",model.fileList.count];
    //static
//    UgcMainListModel *model = self.arrM[indexPath.row];
    
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];

    if ([modelSub.relType isEqualToString:@"1"]) {
        static NSString *sectionCell = @"MainFirst_MagazineList_UgcCell_WX";
        MainFirst_MagazineList_UgcCell_WX *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[MainFirst_MagazineList_UgcCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.State = 1;
        cell.model = modelSub;
//        if ([kUSER_INFO_MANAGER.userId isEqualToString:self.userId]) {
            [cell setHiddenUserInfo];
//        }
        return cell;
        
    }else{

        static NSString *sectionCell = @"MainFirst_MagazineList_PGCCell_WX";
        MainFirst_MagazineList_PGCCell_WX *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[MainFirst_MagazineList_PGCCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.model = modelSub;
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
    if ([modelSub.relType isEqualToString:@"1"]) {
        Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
        vc.ugcId = modelSub.relId;
        vc.userID = modelSub.userId;
        [vc setBlockDel:^{
            [self.model.result.list removeObjectAtIndex:self.selectCellIndexPath];
            [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];
        }];
        [self.getNav pushViewController:vc animated:YES];
    }else{
        MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
        vc.pgcId = modelSub.relId;
        [vc setBlockDel:^{
            [self.model.result.list removeObjectAtIndex:self.selectCellIndexPath];
            [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];
        }];
        [self.getNav pushViewController:vc animated:YES];
    }
    self.selectCellIndexPath = indexPath.row;
}


-(Ugc_HomePage_TbvHead_WX *)HeadView{
    if (!_HeadView) {
        _HeadView = [[Ugc_HomePage_TbvHead_WX alloc]initWithFrame:CGRectMake(0, 0, kScreenW,90+20)];
        _HeadView.backgroundColor = [UIColor whiteColor];
    }
    return _HeadView;
}

-(void)setUserId:(NSString *)userId{
    _userId = userId;
    if (![userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
        self.tableHeaderView = self.HeadView;
        [self loadUserInfo];
    }
    [self setLoad];
    
}

-(void)setUserName:(NSString *)userName{
    _userName = userName;
    self.HeadView.labUserName.text = userName;
}

-(void)setUserAvatar:(NSString *)userAvatar{
    _userAvatar = userAvatar;
    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,userAvatar];
    [self.HeadView.PicUser sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    NSLog(@"ste =  = %@",str);
    
}

-(void)loadUserInfo{
    
    [[serviceDefualt loadMainUserInfomainUserInfo:self.userId reponseObjctClass:[MyselfFollowListModelSub class]]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            self.HeadView.modelSub = x.responseData;
//            [self beginUpdates];
            [self setTableHeaderView:self.HeadView];
//            [self endUpdates];
        }else{
            [Tools showTips:x.errorInfo];
        }
        NSLog(@"");
        
    }];
    
}
@end
